1 00:00:00,800 --> 00:00:01,730 Welcome back. 2 00:00:01,730 --> 00:00:02,660 In this lecture. 3 00:00:02,660 --> 00:00:06,680 We're going to be working on the sprinting system for our players. 4 00:00:06,680 --> 00:00:11,600 If you remember we have a stamina guy inside of starter guy here. 5 00:00:11,900 --> 00:00:16,160 And this guy is going to be representing how much stamina a player has left. 6 00:00:16,160 --> 00:00:20,450 So when a player gets a character, we need to listen for when that player sprints and when they do, 7 00:00:20,480 --> 00:00:24,200 we should increase their speed and start taking away stamina. 8 00:00:24,200 --> 00:00:28,730 If you were in my previous course, the sprinting system we're going to be making here is basically 9 00:00:28,730 --> 00:00:33,380 going to be the exact same one as we created in the project for that previous course. 10 00:00:33,380 --> 00:00:38,060 So let's go ahead and get started inside of starter character scripts, we're going to open our sprint 11 00:00:38,060 --> 00:00:39,290 guy handler. 12 00:00:39,290 --> 00:00:42,260 And inside of here we're going to need four services. 13 00:00:42,260 --> 00:00:44,510 One is going to be the player service. 14 00:00:45,740 --> 00:00:49,250 We're going to need a service called the Context Action Service. 15 00:00:49,250 --> 00:00:52,880 That way we can listen to when a specific key is pressed. 16 00:00:55,070 --> 00:01:02,030 We're going to need the tween service to tween UI elements, and we're also going to need the run service 17 00:01:02,030 --> 00:01:05,750 because we're going to be updating the stamina bar basically every frame. 18 00:01:06,860 --> 00:01:09,830 Now some references we're going to have to make to stuff in here. 19 00:01:09,830 --> 00:01:12,230 One is going to be the players camera. 20 00:01:12,230 --> 00:01:16,850 So workspace dot current camera because when the player sprints we want to give the effect that they're 21 00:01:16,850 --> 00:01:19,520 moving faster by increasing their field of view. 22 00:01:19,520 --> 00:01:24,740 And then when they stop sprinting, we can return the field of view on the camera back to its previous 23 00:01:24,740 --> 00:01:25,310 one. 24 00:01:25,310 --> 00:01:27,950 We want to have a reference to the local player. 25 00:01:28,690 --> 00:01:30,970 Because we need to get the player's character. 26 00:01:30,970 --> 00:01:38,470 So player dot character or we can do player dot character added weight. 27 00:01:38,470 --> 00:01:43,450 This is always a good practice to do, even though it's kind of redundant in here because the script 28 00:01:43,450 --> 00:01:45,490 is inside of starter character scripts. 29 00:01:45,490 --> 00:01:50,710 We don't have to actually wait for the character to be added, but I always include it anyways, because 30 00:01:50,710 --> 00:01:55,630 there will be times in other scripts where you might need to actually wait for the character to replicate, 31 00:01:55,630 --> 00:02:00,700 and if you don't ever include it, you might forget to do it and you'll run into issues later down the 32 00:02:00,700 --> 00:02:01,090 line. 33 00:02:01,090 --> 00:02:02,980 So I just include it just in case. 34 00:02:02,980 --> 00:02:08,530 But you're not going to have to for any script that is inside of starter character scripts. 35 00:02:08,920 --> 00:02:13,420 And the next we're going to make a reference to the humanoid, and we can denote it as the humanoid. 36 00:02:13,420 --> 00:02:16,870 So character weight for child humanoid. 37 00:02:18,610 --> 00:02:20,950 We're going to make a reference to our sprinting guy. 38 00:02:20,950 --> 00:02:22,480 So we'll just call it guy. 39 00:02:22,480 --> 00:02:25,030 And that's inside of the players player guy folder. 40 00:02:25,030 --> 00:02:27,760 And we need to wait for this guy to replicate. 41 00:02:28,430 --> 00:02:31,340 And then we're going to create some other variables. 42 00:02:31,340 --> 00:02:33,830 One is going to keep track of our current stamina. 43 00:02:33,830 --> 00:02:36,980 And right now we'll set it to 100. 44 00:02:36,980 --> 00:02:39,110 We're also going to create. 45 00:02:39,810 --> 00:02:45,360 A variable to keep track of the camera's original field of view, so we'll call it a ridge FOV, which 46 00:02:45,360 --> 00:02:48,600 is equal to the camera dot field of view. 47 00:02:48,900 --> 00:02:53,160 And then we're going to have some other booleans in here, one that's going to tell us whether or not 48 00:02:53,160 --> 00:02:55,470 the player has their shift key down. 49 00:02:55,470 --> 00:02:57,240 And we'll set that to false for now. 50 00:02:57,270 --> 00:03:02,370 Another one is we're going to have a boolean to keep track of a sprinting cooldown. 51 00:03:02,370 --> 00:03:04,770 So sprinting cooldown. 52 00:03:05,810 --> 00:03:06,890 Is equal to false. 53 00:03:06,890 --> 00:03:11,240 And then we're going to have a Boolean to tell us whether or not we can replenish stamina. 54 00:03:11,240 --> 00:03:14,720 So can replenish stamina. 55 00:03:14,720 --> 00:03:17,300 And we'll set that to true. 56 00:03:17,510 --> 00:03:23,660 Now another thing I'm going to do is I'm going to store a function, a global function in local memory. 57 00:03:23,660 --> 00:03:25,970 And that's for creating a new Udim two. 58 00:03:25,970 --> 00:03:28,610 So we're going to call this new Udim two. 59 00:03:28,610 --> 00:03:31,850 And it's going to be equal to the Udim two dot new function. 60 00:03:31,850 --> 00:03:38,120 And this is because since we're going to be updating the stamina guy basically every single frame, 61 00:03:38,120 --> 00:03:44,030 it's more performant for us to access a function in local memory than it is for us to grab it from a 62 00:03:44,030 --> 00:03:50,750 global memory, or basically trying to get this global function from our game's data model. 63 00:03:50,750 --> 00:03:53,600 Now we're going to also define some constants here. 64 00:03:53,750 --> 00:03:58,910 One constant is going to be the speed at which we will be sprinting. 65 00:03:58,910 --> 00:04:02,690 So in this case our sprinting speed will be 24 studs per second. 66 00:04:02,720 --> 00:04:05,750 We'll store the regular speed of our player's character. 67 00:04:05,750 --> 00:04:08,930 So that'll be humanoid dot walk speed. 68 00:04:09,470 --> 00:04:14,360 We want to store the max stamina a player can have, which would be 100. 69 00:04:14,540 --> 00:04:21,470 We want to store how much to reduce our stamina by, and this number is going to represent how much 70 00:04:21,470 --> 00:04:25,160 stamina to remove from our current stamina every second. 71 00:04:25,160 --> 00:04:28,400 So for example, I found that the number of 18 is good. 72 00:04:28,400 --> 00:04:35,930 So every second will have 18 numbers removed from our 100, uh, when we're sprinting. 73 00:04:36,350 --> 00:04:42,110 And then we'll also create a variable for increasing the stamina or recovering the stamina. 74 00:04:42,110 --> 00:04:46,820 And you'll want to make sure that this number is lower than reducing the stamina, because we want to 75 00:04:46,820 --> 00:04:51,050 make it harder for them to regain stamina than to lose stamina. 76 00:04:51,080 --> 00:04:55,850 Then we're going to create a constant that will represent the field of view for when we're sprinting. 77 00:04:55,850 --> 00:04:57,980 And that's going to be 85 degrees. 78 00:04:57,980 --> 00:05:05,300 And then we're also going to, uh, create a constant that will represent the cooldown for when a player 79 00:05:05,300 --> 00:05:06,830 reaches zero stamina. 80 00:05:06,830 --> 00:05:08,480 And we're going to set that to five seconds. 81 00:05:08,480 --> 00:05:13,340 So if a player is sprinting and their stamina goes all the way down to zero, then we want to wait for 82 00:05:13,340 --> 00:05:16,910 five seconds before we can actually start replenishing stamina again. 83 00:05:17,300 --> 00:05:22,250 So in here, the only private function we're going to need is a function to update our stamina bar. 84 00:05:22,250 --> 00:05:24,470 And we're going to call it update stamina bar. 85 00:05:25,140 --> 00:05:29,370 And then the events we're going to have to listen into here are only two. 86 00:05:29,400 --> 00:05:32,760 The first event is going to be within the Context Action Service. 87 00:05:32,760 --> 00:05:36,600 And what we want to do is we want to bind an action to a particular key. 88 00:05:36,600 --> 00:05:40,950 And the name we're going to give for this action is shift to sprint. 89 00:05:41,560 --> 00:05:45,400 And the function we want to bind will be a lambda function here. 90 00:05:45,820 --> 00:05:49,240 And then there's some other stuff we have to pass to this function. 91 00:05:49,240 --> 00:05:52,480 One will be a boolean for creating a touch button. 92 00:05:52,480 --> 00:05:54,400 And this will be on mobile devices. 93 00:05:54,400 --> 00:05:58,690 But since our game isn't going to support mobile devices, I'm just going to set this to false for now. 94 00:05:58,960 --> 00:06:04,240 And then we need to pass the key that we need to attach this action to. 95 00:06:04,240 --> 00:06:09,070 And that's going to be enum dot Keycode dot left shift. 96 00:06:09,070 --> 00:06:12,970 So when a player holds down the left shift key then we're going to start sprinting. 97 00:06:13,800 --> 00:06:20,010 So that means inside of this lambda function, we're going to get passed the name of the action and 98 00:06:20,010 --> 00:06:23,040 then the input state for our player. 99 00:06:23,040 --> 00:06:28,650 And what we're going to do in this function is we're going to make sure that the input state is in the 100 00:06:28,650 --> 00:06:29,640 beginning stages. 101 00:06:29,640 --> 00:06:36,810 So if input state is equal to enum dot user input state dot begin. 102 00:06:36,810 --> 00:06:39,540 Or basically we're beginning to push the key down. 103 00:06:39,870 --> 00:06:43,830 Then what we need to do is we need to set our shift down variable equal to true. 104 00:06:45,890 --> 00:06:55,820 Otherwise, if our input state is equal to the enum dot user input state dot end, then we're going 105 00:06:55,820 --> 00:06:57,710 to set shift down equal to false. 106 00:06:57,710 --> 00:07:02,390 So when a player presses down a key, it'll give us the begin input state. 107 00:07:02,390 --> 00:07:06,560 And when a player lifts up the key it'll give us the end input state. 108 00:07:06,860 --> 00:07:11,570 Now one other thing we're going to have to make sure is that we actually have enough stamina to set 109 00:07:11,570 --> 00:07:13,370 this variable to true here. 110 00:07:13,370 --> 00:07:19,700 So if our current stamina is less than or equal to zero, then we're just going to return out of this 111 00:07:19,700 --> 00:07:20,420 function. 112 00:07:20,660 --> 00:07:24,080 Now the next event we need to listen to is inside of our run service. 113 00:07:24,080 --> 00:07:25,670 And that's the heartbeat event. 114 00:07:25,670 --> 00:07:30,170 So every single heartbeat we're going to be updating our sprinting guy. 115 00:07:31,260 --> 00:07:37,140 And this function is going to get passed something called a delta time, or basically this number represents 116 00:07:37,140 --> 00:07:40,320 the amount of time between frames. 117 00:07:40,320 --> 00:07:47,070 So when this event gets fired, it'll get passed the amount of time it took from the last time this 118 00:07:47,070 --> 00:07:47,820 event fired. 119 00:07:47,820 --> 00:07:50,190 So let's say this event gets fired right? 120 00:07:50,190 --> 00:07:51,540 And then it gets fired again. 121 00:07:51,570 --> 00:07:55,860 The difference in time between those two events firing is what gets passed to this function. 122 00:07:55,860 --> 00:07:57,630 And we're going to call it Delta time. 123 00:07:58,020 --> 00:08:03,240 Now what we have to do in here is we need to check whether or not our shift is being held down. 124 00:08:03,240 --> 00:08:07,200 And if it is, we need to start sprinting and reducing stamina. 125 00:08:07,200 --> 00:08:12,660 So if our shift key is being held down, and we also want to make sure that our current stamina is greater 126 00:08:12,660 --> 00:08:18,450 than zero, and we also want to make sure our player is actually moving, because if the player isn't 127 00:08:18,450 --> 00:08:21,330 moving, let's say they're just sitting there and they're holding the shift key down. 128 00:08:21,330 --> 00:08:23,790 It's going to reduce stamina even if we're not moving. 129 00:08:23,790 --> 00:08:30,180 So we need to make sure our humanoid dot move direction dot magnitude is greater than zero. 130 00:08:31,120 --> 00:08:36,670 Now, the first thing we should check is whether or not our camera's field of view is set to the sprint 131 00:08:36,670 --> 00:08:37,420 field of view. 132 00:08:37,420 --> 00:08:41,890 Because if it isn't, then we need to zoom our camera out to give the illusion that we're moving faster. 133 00:08:41,890 --> 00:08:48,820 So if the camera dot field of view is not equal to the sprint field of view, then what we need to do 134 00:08:48,820 --> 00:08:50,380 is we need to create a tween. 135 00:08:50,380 --> 00:08:54,220 We're going to use the tween service and create a tween on our camera. 136 00:08:54,850 --> 00:08:59,350 And we can make this tween last something like 0.8 seconds. 137 00:08:59,560 --> 00:09:03,070 And what we need to change on our camera is the field of view. 138 00:09:03,070 --> 00:09:06,190 And we're going to set that equal to the sprint field of view. 139 00:09:06,460 --> 00:09:08,920 And then we can just play this tween. 140 00:09:09,530 --> 00:09:10,520 Afterwards. 141 00:09:10,520 --> 00:09:14,360 While we are sprinting, we want to continue to make sure that the humanoids walk. 142 00:09:14,360 --> 00:09:20,930 Speed is equal to the sprint speed, and then we also want to make sure that we're reducing the stamina 143 00:09:20,930 --> 00:09:22,640 of our player so we can do current. 144 00:09:22,640 --> 00:09:28,190 Stamina is minus equal to our stamina reduce. 145 00:09:28,190 --> 00:09:31,610 And then we're going to multiply this by our delta time. 146 00:09:31,760 --> 00:09:37,130 Because basically what's going to be happening is that there may be some inconsistencies when this event 147 00:09:37,130 --> 00:09:42,110 gets fired, because some players might be running the game at higher frame rates than other players. 148 00:09:42,110 --> 00:09:48,440 And if that's the case, we still want to decrease the stamina by the same amount, or we want to take 149 00:09:48,440 --> 00:09:51,530 the same amount of time to reduce our stamina. 150 00:09:51,530 --> 00:09:55,130 So that way it's the same for every player, regardless of their frame rate. 151 00:09:55,130 --> 00:10:01,190 So that's why it's important that we multiply our stamina reduce by the delta time, or aka how much 152 00:10:01,190 --> 00:10:03,470 time it took between the heartbeats. 153 00:10:03,620 --> 00:10:07,040 So for example, let's say the player's frame rate all of a sudden drops. 154 00:10:07,040 --> 00:10:12,230 Then that means the delta time is going to increase, and when the delta time increases, that means 155 00:10:12,230 --> 00:10:17,060 the amount that we're going to reduce our current stamina by is also going to increase. 156 00:10:17,600 --> 00:10:22,460 Otherwise, if we don't have our shift key held down, then we need to reset the field of view of our 157 00:10:22,460 --> 00:10:23,120 camera. 158 00:10:23,120 --> 00:10:30,800 So if the camera dot field of view is not equal to the original field of view, then we need to again 159 00:10:30,800 --> 00:10:32,990 create a tween on our camera. 160 00:10:34,040 --> 00:10:36,890 It'll take about we can do 0.8 seconds again. 161 00:10:36,890 --> 00:10:41,690 So tween info dot new 0.8 seconds. 162 00:10:41,690 --> 00:10:45,350 And then we'll set the camera's field of view. 163 00:10:46,210 --> 00:10:50,920 Field of view equal to the original field of view. 164 00:10:50,920 --> 00:10:53,260 And then we'll make sure to play this tween. 165 00:10:53,650 --> 00:10:59,110 Afterwards we want to make sure to reset the humanoids walk speed back to the regular speed. 166 00:10:59,850 --> 00:11:05,070 And then we want to check whether or not our current stamina is equal to zero or less. 167 00:11:05,070 --> 00:11:09,660 So if our current stamina is less than or equal to zero. 168 00:11:11,270 --> 00:11:16,670 Then what we need to do is we need to enforce our cool down basically. 169 00:11:16,670 --> 00:11:22,220 So we're going to set current stamina to zero, and then we're going to check if sprinting cooldown 170 00:11:22,220 --> 00:11:22,910 is set to true. 171 00:11:22,910 --> 00:11:26,690 If it is then we're going to return, because then what we're going to do is we're going to set sprinting 172 00:11:26,690 --> 00:11:28,250 cooldown equal to true. 173 00:11:28,280 --> 00:11:31,670 We're going to set can replenish stamina equal to false. 174 00:11:32,740 --> 00:11:37,630 We're going to set shift down equal to false just in case. 175 00:11:37,630 --> 00:11:41,260 And then we're going to wait for our sprint to cool down. 176 00:11:42,480 --> 00:11:48,990 Now, it's very important that we have this variable here to block any other threads of execution from 177 00:11:48,990 --> 00:11:53,820 running this code down here, because this function is going to be executed many times per second. 178 00:11:53,820 --> 00:11:54,060 All right. 179 00:11:54,060 --> 00:11:56,040 This is firing for every single frame. 180 00:11:56,040 --> 00:11:59,580 So we need to block any other threads from executing. 181 00:11:59,760 --> 00:12:05,970 That way we don't have any, you know, conflicting threads trying to edit settings or stamina at the 182 00:12:05,970 --> 00:12:06,900 same time. 183 00:12:06,900 --> 00:12:13,500 Otherwise, after we wait for our sprint cooldown, then we can set can replenish stamina equal to true. 184 00:12:13,860 --> 00:12:18,870 And once that's done, we can have another if statement down here for regaining the stamina of the player. 185 00:12:18,870 --> 00:12:23,070 So if the current stamina of our player is less than our max stamina. 186 00:12:23,630 --> 00:12:28,730 Then what we need to do is we need to check whether or not we can actually replenish stamina. 187 00:12:28,760 --> 00:12:32,960 So if we cannot replenish stamina, then we just need to return. 188 00:12:33,720 --> 00:12:34,680 Otherwise. 189 00:12:34,680 --> 00:12:37,830 We also need to check whether or not the player is sprinting. 190 00:12:38,130 --> 00:12:40,470 So if we are not sprinting. 191 00:12:41,720 --> 00:12:50,420 Or our humanoid move direction dot magnitude is less than or equal to zero. 192 00:12:50,870 --> 00:12:58,040 Then we can start to regain stamina so our current stamina can be set to itself. 193 00:12:58,040 --> 00:13:01,700 So plus equal stamina increase. 194 00:13:01,700 --> 00:13:05,270 And then again we have to multiply this number by our delta time. 195 00:13:05,270 --> 00:13:10,730 That way if players have different frame rates, they will still be regaining stamina at the same pace 196 00:13:10,730 --> 00:13:11,810 as another player. 197 00:13:12,110 --> 00:13:17,240 And then we're also going to make sure to set sprinting cooldown equal to false here. 198 00:13:17,240 --> 00:13:23,270 So that way we can go back into this if statement and do our sprint cooldown again when we need to. 199 00:13:23,570 --> 00:13:29,750 And then at the very end, what we can do is we can update our stamina bar. 200 00:13:30,080 --> 00:13:37,070 So what we need to do in this function is we first need to check whether or not our guy is enabled, 201 00:13:37,070 --> 00:13:42,890 because by default, our stamina guy is not going to be enabled and we need to enable it. 202 00:13:42,890 --> 00:13:48,290 And then we need to fade these two different elements onto the screen. 203 00:13:48,290 --> 00:13:50,600 So if I actually enable the transparency here. 204 00:13:50,600 --> 00:13:54,740 So with the border there's an outline here that I need to enable the transparency for. 205 00:13:54,740 --> 00:13:56,120 So we'll make that opaque. 206 00:13:56,120 --> 00:13:59,870 And then we also have a fill in here that we need to enable the transparency for. 207 00:14:00,260 --> 00:14:06,020 So the idea is when the player sprints this fill bar is going to go down until it hits zero. 208 00:14:06,020 --> 00:14:09,020 And then it's going to slowly replenish again. 209 00:14:09,170 --> 00:14:13,190 And then once it's fully replenished then we're going to fade this guy back out. 210 00:14:14,310 --> 00:14:20,970 What we need to do is set this back to be transparent again, and then we need to disable this guy. 211 00:14:20,970 --> 00:14:25,890 And then we're going to go back in here and we're going to check if our guy is enabled or not. 212 00:14:25,890 --> 00:14:31,980 So if not guy dot enabled then we need to set guy dot enabled equal to true. 213 00:14:31,980 --> 00:14:43,020 And then we need to fade those two UI elements onto the screen so we can create a tween on our guy dot 214 00:14:43,020 --> 00:14:44,580 border dot outline. 215 00:14:44,580 --> 00:14:48,090 So this UI stroke here we need to fade this in. 216 00:14:48,090 --> 00:14:53,070 And we're going to do tween info dot new and can do something like 0.3 seconds. 217 00:14:53,070 --> 00:14:57,150 And we need to change the transparency equal to zero. 218 00:14:57,150 --> 00:14:58,230 So it's fully opaque. 219 00:14:58,230 --> 00:14:59,910 And then we can play this tween. 220 00:15:00,150 --> 00:15:03,210 And then we also need to fade in that fill bar. 221 00:15:03,210 --> 00:15:08,700 So guy dot border dot fill again tween info dot new. 222 00:15:08,700 --> 00:15:10,170 We'll do 0.3 seconds. 223 00:15:10,170 --> 00:15:14,970 And the property we need to change on this one is the background transparent agency. 224 00:15:15,330 --> 00:15:17,280 And that needs to be set to be opaque. 225 00:15:17,280 --> 00:15:18,810 And then we can play that tween. 226 00:15:19,550 --> 00:15:27,080 Now, the next thing we need to do is we need to set the size of our fill frame here. 227 00:15:27,080 --> 00:15:30,230 So gui dot border, dot fill. 228 00:15:30,230 --> 00:15:38,510 We need to set the size equal to a new Udim two that represents, um, the fraction or the percentage 229 00:15:38,510 --> 00:15:39,590 of our stamina. 230 00:15:39,590 --> 00:15:46,160 So what we're going to do is we're going to divide our max stamina by our current stamina to get that 231 00:15:46,160 --> 00:15:47,540 percentage right. 232 00:15:47,540 --> 00:15:55,310 So if our current stamina is 50 and our max stamina is 100, then that means our fill bar should be 233 00:15:55,310 --> 00:15:57,800 50% or halfway filled up. 234 00:15:57,800 --> 00:16:05,810 So that means what we need to do here is we need to set the width on the, uh, or the scale width on 235 00:16:05,810 --> 00:16:07,070 the x axis. 236 00:16:07,070 --> 00:16:08,240 That needs to be one. 237 00:16:08,240 --> 00:16:12,470 By default, we want to set the offset on the x axis to zero. 238 00:16:12,470 --> 00:16:18,920 And then for the offset on the y axis, we need to set that to the current stamina divided by our max 239 00:16:18,920 --> 00:16:20,960 stamina to get the percentage. 240 00:16:21,020 --> 00:16:25,370 And then we can set zero for the y axis offset. 241 00:16:26,080 --> 00:16:33,580 And then once we do that, then we can check if the g dot boarder dot Philsys dot y becomes greater 242 00:16:33,580 --> 00:16:35,140 than or equal to one. 243 00:16:35,140 --> 00:16:43,390 And we also need to make sure that the g y dot boarder dot outline dot transparency is less than or 244 00:16:43,390 --> 00:16:45,010 equal to zero. 245 00:16:45,130 --> 00:16:51,880 So basically what's going on here is that our stamina has fully replenished and the guy is still visible 246 00:16:51,880 --> 00:16:52,840 on our screen. 247 00:16:52,840 --> 00:16:57,520 So in that case we need to fade out our guy to no longer be visible. 248 00:16:57,820 --> 00:17:04,720 So what we're going to do is we're going to use the tween service again and create a tween on our guy 249 00:17:04,750 --> 00:17:06,430 dot border dot outline. 250 00:17:06,850 --> 00:17:10,090 We can do the same time of 0.3 seconds. 251 00:17:10,090 --> 00:17:14,740 And we need to set the transparency to be fully transparent. 252 00:17:14,740 --> 00:17:16,510 And then we can play this tween. 253 00:17:16,510 --> 00:17:23,050 And then basically we can do the exact same thing for the fill as well and set this to be fully transparent. 254 00:17:23,670 --> 00:17:27,570 And what am I going to do is I'm actually going to store the return from this function. 255 00:17:28,290 --> 00:17:30,090 So I'm just going to call this tween. 256 00:17:30,420 --> 00:17:32,670 And then we can play this tween. 257 00:17:32,670 --> 00:17:37,680 And the reason I made a reference to it is because I want to listen for when this tween completes. 258 00:17:37,680 --> 00:17:39,960 So tween dot completed we're going to wait. 259 00:17:40,530 --> 00:17:45,810 And once the tween is completed, then we're going to set GUI dot enabled equal to false. 260 00:17:46,230 --> 00:17:52,440 Now, because we are yielding here for 0.3 seconds, there might be a time where the player will sprint 261 00:17:52,440 --> 00:17:54,210 in the middle of that wait statement. 262 00:17:54,210 --> 00:17:59,430 So before we actually disable the visibility or the enable property for our GUI. 263 00:17:59,460 --> 00:18:04,800 We want to make sure that our stamina is still filled so we can copy this here again. 264 00:18:04,890 --> 00:18:10,830 And if the GUI border filled out, size dot y is still greater than or equal to one, then we can go 265 00:18:10,830 --> 00:18:13,590 ahead and disable our GUI. 266 00:18:13,860 --> 00:18:14,820 And guess what? 267 00:18:14,820 --> 00:18:17,580 Our stamina system should be complete. 268 00:18:17,580 --> 00:18:21,750 So if we go to our main story and we play test this. 269 00:18:22,460 --> 00:18:26,210 We should be able to see our stamina bar appear when we sprint. 270 00:18:26,210 --> 00:18:31,010 So if I hold down shift right now, as you can see, our stamina bar is not showing. 271 00:18:31,010 --> 00:18:33,830 But as soon as I start walking around, it should appear. 272 00:18:34,370 --> 00:18:35,390 And there we go. 273 00:18:35,840 --> 00:18:39,440 It's moving around now we are getting an error in the console. 274 00:18:39,710 --> 00:18:42,740 Attempt to compare number greater than or equal to a udim. 275 00:18:42,740 --> 00:18:44,930 So let's go ahead and look at this real quick. 276 00:18:46,010 --> 00:18:47,300 So. 277 00:18:49,300 --> 00:18:51,370 Let me stop this and go to the error. 278 00:18:52,070 --> 00:18:53,450 Where we can actually just open it up. 279 00:18:53,450 --> 00:18:54,620 It's on line 49. 280 00:18:54,620 --> 00:18:58,310 So this line right here, we are getting an error. 281 00:18:58,520 --> 00:19:03,500 Oh, and that's because we actually need to grab the value off of this. 282 00:19:03,500 --> 00:19:03,920 Why. 283 00:19:03,920 --> 00:19:05,570 Because there are two different values. 284 00:19:05,570 --> 00:19:07,130 There's an offset and there's a scale. 285 00:19:07,130 --> 00:19:11,660 So right now we're just trying to compare the udim itself to a number which isn't going to work. 286 00:19:11,660 --> 00:19:16,100 We actually need to grab the scale of this y axis. 287 00:19:16,100 --> 00:19:18,890 And then we need to do the same thing down here okay. 288 00:19:18,890 --> 00:19:19,940 Our error should be fixed. 289 00:19:19,940 --> 00:19:21,590 Let's go ahead and try that out again. 290 00:19:23,880 --> 00:19:27,900 So if we start to sprint, as you can see, our stamina goes down. 291 00:19:27,900 --> 00:19:29,580 If I stop moving. 292 00:19:30,790 --> 00:19:35,650 The stamina starts increasing, but it looks like we got another error on line 53. 293 00:19:35,680 --> 00:19:37,480 Attempt to compare instance. 294 00:19:37,750 --> 00:19:40,030 Uh, less than a number. 295 00:19:40,030 --> 00:19:42,460 So let's go ahead and look at this problem here. 296 00:19:42,460 --> 00:19:43,000 Oh. 297 00:19:43,530 --> 00:19:47,730 Looks like again, we did a little bit of a mistype. 298 00:19:47,730 --> 00:19:52,890 We got to type in walk speed here, not the humanoid, but we need to compare the walk speed of the 299 00:19:52,890 --> 00:19:53,760 humanoid. 300 00:19:53,790 --> 00:19:55,140 Okay, another error fixed. 301 00:19:55,140 --> 00:19:56,940 Let's go ahead and try that again. 302 00:20:00,120 --> 00:20:00,960 Okay. 303 00:20:01,620 --> 00:20:06,510 And actually I think that came from did that come from or that came from our footstep handler? 304 00:20:06,720 --> 00:20:07,170 Okay. 305 00:20:07,170 --> 00:20:08,400 Gotcha, gotcha. 306 00:20:08,910 --> 00:20:12,330 Uh, make sure you fix this error inside of your footsteps handler. 307 00:20:12,810 --> 00:20:15,330 That is, uh, that is an error you will want to fix. 308 00:20:15,330 --> 00:20:16,050 Okay. 309 00:20:16,690 --> 00:20:18,370 Now we should be good to go. 310 00:20:18,370 --> 00:20:23,560 So if I go and sprint, our stamina is increasing and we're running around. 311 00:20:24,100 --> 00:20:30,550 And then once our stamina reaches zero, as you can see, we are no longer sprinting and it's not going 312 00:20:30,550 --> 00:20:32,290 to regenerate after those five seconds. 313 00:20:32,290 --> 00:20:34,090 As you can see, it's regenerating again. 314 00:20:34,090 --> 00:20:38,470 And if I hold down shift, I'm able to sprint around and decrease the stamina. 315 00:20:38,500 --> 00:20:40,330 Then it goes back down to zero. 316 00:20:40,600 --> 00:20:46,060 We can't sprint, we can't do anything, and we're going to have to wait for this to slowly fill back 317 00:20:46,060 --> 00:20:46,660 up. 318 00:20:48,550 --> 00:20:50,890 And let's go ahead and see if our guy fades out. 319 00:20:50,890 --> 00:20:52,510 So when it reaches the max, there we go. 320 00:20:52,510 --> 00:20:53,650 Our guy fades out. 321 00:20:53,650 --> 00:20:57,790 And then when we start to sprint again, then it reappears back onto our screen. 322 00:20:59,320 --> 00:21:03,580 So that's how you can create a simple sprinting system in your games. 323 00:21:03,610 --> 00:21:07,000 Let's keep working on this project and I'll see you in the next lecture.